/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package domain.controllers;

import domain.Entity.EntityDireccion;
import domain.Entity.EntityGestor;
import domain.Entity.EntityPerfil;
import domain.Entity.EntityUsuario;
import domain.commands.RegistrarComando;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import managers.UsuarioManager;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;
/**
 *
 * @author jorge
 */
public class registroController extends SimpleFormController {

    
    UsuarioManager um = UsuarioManager.getInstance();
    
    public registroController() {
        setCommandClass(RegistrarComando.class);
  }

    @Override
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, org.springframework.validation.BindException errors) throws Exception {

    
        ModelAndView mav = new ModelAndView();
        
        RegistrarComando rc = (RegistrarComando) command;
        
        long dni = Integer.parseInt( rc.getDni());
        
        if(estaUsuario(rc.getNombre()) || estaDni(dni)){
            
            mav.setView(new RedirectView("errorNombre.htm"));
        }else{
  
            um.addGestor(dni,rc.getEmail(),rc.getContrasena(),rc.getNombre());
            
            mav.setView(new RedirectView("login.htm"));
        }
        
        return mav;
        
    }
    
    private boolean estaUsuario(String nombre){
        
         EntityUsuario eu = um.get(nombre);
         
         return (eu.getNombre() != null) ;
        
    }
    
    private boolean estaDni(long dni){
        
         EntityUsuario eu = um.obetenerGestor(dni);
         
         return (eu != null) ;
        
    }

}
